﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using BankCreditPortfolio.Models.HomeModels;
using BankCreditPortfolio.DAL;
using BankCreditPortfolio.DAL.Entities;
using BankCreditPortfolio.DAL.Services;
using MvcContrib.Pagination;

namespace BankCreditPortfolio.Controllers
{
    public class HomeController : BaseController
    {
        public ActionResult Index()
        {
            var accountService = new AccountService();
            var user = Request.RequestContext.HttpContext.User.Identity.Name;
            if (accountService.GetRoleByEmail(user) != null)
            {
                if (accountService.GetRoleByEmail(user).Title == "Admin")
                {
                    return RedirectToAction("Index", "Admin");
                }
                if (accountService.GetRoleByEmail(user).Title == "Client")
                {
                    return RedirectToAction("Tariffs", "Home");
                }
                return RedirectToAction("Orders", "Bank");
            }

            return RedirectToAction("Tariffs", "Home");
        }

        public ActionResult Tariffs(int? page)
        {
            var tariffs = (new TariffService()).GetAll().Where(x => x.Used);
            IEnumerable<Tariff> tariffItems = tariffs != null ? tariffs.ToList() : new List<Tariff>();
            return View(tariffItems.AsPagination(page ?? 1, 10));
        }

        public ActionResult Tariff(int tariffId)
        {
            var tariff = (new TariffService()).Find(tariffId);
            var model = new TariffModel
                            {
                                Currency = tariff.Currency.Title,
                                Description = tariff.Description,
                                Percent = tariff.CreditPercent,
                                Fine = tariff.Fine,
                                Max = tariff.Max,
                                Min = tariff.Min,
                                FirstPayment = tariff.FirstPayment,
                                OtherPayments = tariff.OtherPayments,
                                RequireGuarantor = tariff.NumberOfGuarantors > 0,
                                Time = tariff.Time,
                                Title = tariff.Title,
                                EarlyRepaimentFine = tariff.EarlyRepaymentFine,
                                Documents = (List<DocumentType>) tariff.Documents
                            };
            return View(model);
        }
    }
}
